package cu.tedu._05mvcboot02.controller;

import cu.tedu._05mvcboot02.common.response.JsonResult;
import cu.tedu._05mvcboot02.common.response.StatusCode;
import cu.tedu._05mvcboot02.mapper.UserMapper;
import cu.tedu._05mvcboot02.pojo.dto.UserDTO;
import cu.tedu._05mvcboot02.pojo.entity.User;
import cu.tedu._05mvcboot02.pojo.vo.UserListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


/**
 * RestController:组合注解,相当于 ResponseBody注解 + Controller注解;
 * 1.加此注解后,类上无需再添加 Controller 注解;
 * 2.加此注解后,控制器方法上无需再添加 ResponseBody 注解.
 */
@Validated
@Slf4j
@Api(tags = "用户模块")
@RestController
@RequestMapping("/v1/users/")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    /**
     * 1.添加用户功能
     */
    @ApiOperation(value = "添加用户")

    @PostMapping("insert")
    public JsonResult addUser(@Validated UserDTO userDTO) {
        log.debug("UserDTO"+userDTO);
        /*
            梳理业务逻辑：确定接口;
         */
        User user = new User();
        //插入数据:一定使用实体类
        //复制属性:addUserDTO ---->  user


        BeanUtils.copyProperties(userDTO, user);
        //设置其他属性的值
        user.setCreated(new Date());
        //调用接口
        userMapper.insertUser(user);

        //return "添加成功";
        return new JsonResult(StatusCode.OPERATION_SUCCESS);
    }

    /**
     * 2.用户列表功能
     */
    @ApiOperation(value = "用户列表功能")
    @GetMapping("userList")

    public JsonResult selectUser() {
        userMapper.selectUser();
        List<UserListVO> userListVOS =userMapper.selectUser();
        return new JsonResult(StatusCode.OPERATION_SUCCESS,userListVOS);
        // return "查询成功" + userMapper.selectUser();
    }

    /**
     * 3.删除指定用户功能
     */

    @GetMapping("delete")
    @ApiOperation(value = "删除指定用户功能")
    @ApiImplicitParam(name = "id",value = "用户编号",required = true)
    public JsonResult delete(@Range(min = 100,max = 200,message = "用户id在100-200之间")@RequestParam int id) {

        if (id<0){
            throw new IllegalArgumentException("Id值无效");
        }
        userMapper.deleteUser(id);
        //return "删除成功";
        return new  JsonResult(StatusCode.OPERATION_SUCCESS);
    }

    /**
     * 4.更新用户功能
     */
    @PostMapping("update")
    @ApiOperation(value = "更新用户功能")

    public JsonResult update(User user) {
        log.debug("User"+user);
        //return "修改成功" + userMapper.updateUserById(user);
        return new JsonResult(StatusCode.OPERATION_SUCCESS);
    }


    /**
     * 5.查询指定用户功能
     *   根据用户ID查询该用户的信息[全字段]
     *   http://localhost:8080/v1/users/用户ID值
     */
    @GetMapping("{id}")
    @ApiOperation(value = "查询指定用户功能")
    public JsonResult selectById(@PathVariable int id){
        User user=userMapper.selectById(id);
        //return userMapper.selectById(id);
        return new JsonResult(StatusCode.OPERATION_SUCCESS,user);
    }
}
